package com.example.barcodeapp.ui.wode.activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.barcodeapp.R;
import com.example.barcodeapp.base.BaseActivity;
import com.example.barcodeapp.interfaces.IBasePersenter;


public class UpDatePhoneNamberActivity extends BaseActivity implements View.OnClickListener {
    private TextView mTvSendVerificationCode;
    private CountDownTimer start = null;
    private EditText mEtPhoneNumber;
    private EditText mEtVerificationCode;
    private TextView mTvEnter;
    private TextView quxiao;
    private Intent intent;

    @Override
    protected int getLayout() {
        return R.layout.activity_up_date_phone_namber;
    }

    @Override
    protected void initView() {
        intent = getIntent();
        mTvSendVerificationCode = findViewById(R.id.tv_send_verification_code);
        mEtPhoneNumber = findViewById(R.id.et_phone_number);//输入的手机号
        mEtVerificationCode = findViewById(R.id.et_verification_code);//输入的验证码
        mTvEnter = findViewById(R.id.tv_enter);
        quxiao = findViewById(R.id.quxiao);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UpDatePhoneNamberActivity.this.finish();

            }
        });
        mEtPhoneNumber.setHintTextColor(Color.parseColor("#B6B6B6"));
        mEtVerificationCode.setHintTextColor(Color.parseColor("#B6B6B6"));
        initListener();
    }

    private void initListener() {
        mTvSendVerificationCode.setOnClickListener(this);
        mTvEnter.setOnClickListener(this);
    }

    @Override
    protected IBasePersenter initPersenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        String phoneNumber = mEtPhoneNumber.getText().toString();
        boolean mobileNO = isMobileNO(phoneNumber);
        switch (v.getId()) {
            case R.id.tv_send_verification_code:
                if (start == null) {
                    if (mobileNO) {
                        start = new CountDownTimer(60 * 1000, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                mTvSendVerificationCode.setText("S " + millisUntilFinished / 1000);
                                mTvSendVerificationCode.setBackgroundResource(R.drawable.shape_send_verification_code1);
                            }

                            @Override
                            public void onFinish() {
                                mTvSendVerificationCode.setText("重新获取验证码");
                                mTvSendVerificationCode.setTextSize(15);
                                start.cancel();
                                start = null;
                            }
                        }.start();
                    } else {
                        Toast.makeText(UpDatePhoneNamberActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.tv_enter:
                String verifcationCode = mEtVerificationCode.getText().toString();
                if (!phoneNumber.isEmpty() && !verifcationCode.isEmpty()) {
                    if (mobileNO) {
                        intent.putExtra("phoneNumber", mEtPhoneNumber.getText().toString());
                        setResult(3, intent);
                        setUpDataSuccessPopupWindow();
                    } else {
                        Toast.makeText(UpDatePhoneNamberActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(UpDatePhoneNamberActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void setUpDataSuccessPopupWindow() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.popupwindow_update_phone_namber_success, null);
        TextView mTvEnter = inflate.findViewById(R.id.tv_enter);
        TextView mTvNewPhoneNumber = inflate.findViewById(R.id.tv_newphonenumber);
        mTvNewPhoneNumber.setText(mEtPhoneNumber.getText().toString());
        int width = getPopupwindoWidth();
        PopupWindow pw = new PopupWindow(inflate, width - 100, WindowManager.LayoutParams.WRAP_CONTENT);
        bgAlpha(0.5f);
        pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                bgAlpha(1f);
            }
        });
        mTvEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pw != null) {
                    pw.dismiss();
                    UpDatePhoneNamberActivity.this.finish();
                }
            }
        });
        pw.showAtLocation(mEtVerificationCode, Gravity.CENTER, 0, 0);
    }

    public void bgAlpha(float bgAlpha) {
        WindowManager.LayoutParams ab = getWindow().getAttributes();
        ab.alpha = bgAlpha;
        getWindow().setAttributes(ab);
    }

    public int getPopupwindoWidth() {
        Display defaultDisplay = getWindowManager().getDefaultDisplay();
        int width = defaultDisplay.getWidth();
        return width;
    }

    /**
     * 验证手机格式
     */
    public static boolean isMobileNO(String mobiles) {
    /*
    移动：134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
    联通：130、131、132、152、155、156、185、186
    电信：133、153、180、189、（1349卫通）
    总结起来就是第一位必定为1，第二位必定为3或5或8，其他位置的可以为0-9
    */
        String telRegex = "[1][3578]\\d{9}";//"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        if (mobiles == null || mobiles.length() == 0 || mobiles.length() != 11)
            return false;
        else
            return mobiles.matches(telRegex);

    }
}